package org.example.java.ref;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * -verbose:gc -Xmx100M
 * Created by juntaozhang on 4/2/15.
 */
public class WeakReferenceListTest {
  public static void main(String[] args) {
    WeakReference<List<byte[]>> weakReference = getListWeakReference();
    List<byte[]> list;
    System.gc();
    list = weakReference.get();
    System.out.println(list != null ? list.size() : null);

  }

  private static WeakReference<List<byte[]>> getListWeakReference() {
    //防止刚出来就被gc new WeakReference<>(new ArrayList())
    List<byte[]> list = new ArrayList<>();
    WeakReference<List<byte[]>> weakReference = new WeakReference<>(list);


    System.gc();
    list = weakReference.get();
    if (list != null) list.add(new byte[1024 * 1024 * 10]);
    System.gc();
    if (list != null) list.add(new byte[1024 * 1024 * 10]);
    System.gc();
    if (list != null) list.add(new byte[1024 * 1024 * 10]);
    System.gc();
    System.out.println(list != null ? list.size() : null);
    return weakReference;
  }
}
